package category

import (
	"time"
)

const (
	// USEC_PER_SEC number of microseconds per second
	USEC_PER_SEC int64 = 1000000
	// MsecPerSec number of milliseconds per second
	MsecPerSec int64 = 1000
	// SecondOfDay 一天的秒数
	SecondOfDay int64 = 24 * 60 * 60
	// MillisecondsOfDay 一天的毫秒数
	MillisecondsOfDay int64 = SecondOfDay * MsecPerSec
	// RealTimenterval 实时数据间隔时间, 单位毫秒
	RealTimenterval int64 = 5 * 1000

	// NullState 正常状态, 字符串"01"
	NullState   = "00"
	NormalState = "01"

	// 历史数据获取的时间, 时, 分, 秒
	HistoryUpdateHour = 17
	HistoryUpdateMin  = 0
	HistoryUpdateSec  = 0

	CACHE_DATE = "20060102"   // 缓存日期
	INDEX_DATE = "2006-01-02" // 索引日期格式
	TDX_DATE   = "20060102"   // 通达信日期
	YearOnly   = "2006"
)

// GetMonthDay 获得当前月的初始和结束日期
func GetMonthDay() (string, string) {
	now := time.Now()
	currentYear, currentMonth, _ := now.Date()
	currentLocation := now.Location()

	firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
	lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
	f := firstOfMonth.Unix()
	l := lastOfMonth.Unix()
	return time.Unix(f, 0).Format("2006-01-02") + " 00:00:00", time.Unix(l, 0).Format("2006-01-02") + " 23:59:59"
}

// GetWeekDay 获得当前周的初始和结束日期
func GetWeekDay() (string, string) {
	now := time.Now()
	offset := int(time.Monday - now.Weekday())
	//周日做特殊判断 因为time.Monday = 0
	if offset > 0 {
		offset = -6
	}

	lastoffset := int(time.Saturday - now.Weekday())
	//周日做特殊判断 因为time.Monday = 0
	if lastoffset == 6 {
		lastoffset = -1
	}

	firstOfWeek := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
	lastOfWeeK := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, lastoffset+1)
	f := firstOfWeek.Unix()
	l := lastOfWeeK.Unix()
	return time.Unix(f, 0).Format("2006-01-02") + " 00:00:00", time.Unix(l, 0).Format("2006-01-02") + " 23:59:59"
}

// GetQuarterDay 获得当前季度的初始和结束日期
func GetQuarterDay(months ...int) (string, string) {
	mn := 0
	if len(months) > 0 {
		mn = months[0]
	}
	now := time.Now().AddDate(0, -mn, 0)
	year := now.Format(YearOnly)
	month := int(now.Month())
	var firstOfQuarter string
	var lastOfQuarter string
	if month >= 1 && month <= 3 {
		//1月1号
		firstOfQuarter = year + "-01-01 00:00:00"
		lastOfQuarter = year + "-03-31 23:59:59"
	} else if month >= 4 && month <= 6 {
		firstOfQuarter = year + "-04-01 00:00:00"
		lastOfQuarter = year + "-06-30 23:59:59"
	} else if month >= 7 && month <= 9 {
		firstOfQuarter = year + "-07-01 00:00:00"
		lastOfQuarter = year + "-09-30 23:59:59"
	} else {
		firstOfQuarter = year + "-10-01 00:00:00"
		lastOfQuarter = year + "-12-31 23:59:59"
	}
	return firstOfQuarter, lastOfQuarter
}
